package com.yy.young.travel.service.impl;

import com.yy.young.common.util.StringUtils;
import com.yy.young.travel.service.ISourceGradeService;
import com.yy.young.common.util.CommonUtil;
import com.yy.young.dal.service.IDataAccessService;
import com.yy.young.dal.util.Page;
import com.yy.young.travel.model.SourceGrade;
import com.yy.young.travel.util.TravelConstants;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.List;

/**
 * 资源评分管理服务实现
 * Created by hua on 2018-03-22.
 */
@Service("sourceGradeService")
public class SourceGradeServiceImpl implements ISourceGradeService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务

    private static final Logger logger = LoggerFactory.getLogger(SourceGradeServiceImpl.class);

    //获取数据列表
    @Override
    public List<SourceGrade> getSourceGradeList(SourceGrade sourceGrade) throws Exception {
        return dataAccessService.getList(TravelConstants.MAPPER.TRAVEL_SOURCE_GRADE + ".getSourceGradeList", sourceGrade);
    }

    //获取数据列表(分页)
    @Override
    public List<SourceGrade> getSourceGradePage(SourceGrade sourceGrade, Page page) throws Exception {
        return dataAccessService.getList(TravelConstants.MAPPER.TRAVEL_SOURCE_GRADE + ".getSourceGradeList", sourceGrade, page);
    }

    //查询单条
    @Override
    public SourceGrade getSourceGrade(String id) throws Exception {
        return (SourceGrade)dataAccessService.getObject(TravelConstants.MAPPER.TRAVEL_SOURCE_GRADE + ".getSourceGradeById", id);
    }

    //修改
    @Override
    public int updateSourceGrade(SourceGrade sourceGrade) throws Exception {
        return dataAccessService.update(TravelConstants.MAPPER.TRAVEL_SOURCE_GRADE + ".update", sourceGrade);
    }

    //批量删除
    @Override
    public int deleteSourceGrade(String[] idArr) throws Exception {
        int i = 0;
        for(String id : idArr){
            i += this.deleteSourceGrade(id);
        }
        return i;
    }

    //删除单条
    @Override
    public int deleteSourceGrade(String id) throws Exception {
        return dataAccessService.delete(TravelConstants.MAPPER.TRAVEL_SOURCE_GRADE + ".delete", id);
    }

    //插入
    @Override
    public int insertSourceGrade(SourceGrade sourceGrade) throws Exception {
        if (StringUtils.isBlank(sourceGrade.getId())){
            sourceGrade.setId(CommonUtil.getUUID());
        }
        return dataAccessService.insert(TravelConstants.MAPPER.TRAVEL_SOURCE_GRADE + ".insert", sourceGrade);
    }

    @Override
    public int batchInsertSourceGrade(List<SourceGrade> list) throws Exception {
        int i = 0;
        if (list != null && list.size() > 0){
            for(SourceGrade sg : list){
                i += this.insertSourceGrade(sg);
            }
        }
        return i;
    }

    @Override
    public int refreshSourceGrade(List<SourceGrade> list, String sourceId) throws Exception {
        if (StringUtils.isNotBlank(sourceId)){
            //先将原始得分记录删除
            this.deleteSourceGradeBySourceId(sourceId);
            //保存新的记录
            return this.batchInsertSourceGrade(list);
        }
        return 0;
    }

    @Override
    public int deleteSourceGradeBySourceId(String sourceId) throws Exception {
        return dataAccessService.delete(TravelConstants.MAPPER.TRAVEL_SOURCE_GRADE + ".deleteSourceGradeBySourceId", sourceId);
    }

}